<?php

class SJB_Contracts_CreateContract extends SJB_Function
{
	public function execute()
	{
		$tp = SJB_System::getTemplateProcessor();
		$invoice_sid = SJB_Request::getVar('invoice_sid', null, false, 'int');
		$invoice = SJB_InvoiceManager::getObjectBySID($invoice_sid);
		$errors = null;
		$userHasContract = false;
		if (!is_null($invoice)) {
			$status = $invoice->getStatus();
			if ($status == SJB_Invoice::INVOICE_STATUS_VERIFIED) {
				$user_sid = $invoice->getPropertyValue('user_sid');
				$items = $invoice->getPropertyValue('items');
				$products = $items['products'];
				$user = SJB_UserManager::getObjectBySID($user_sid);
				$userHasContract = $user->hasContract();
				$paymentStatus   = false;
				foreach ($products as $key => $product) {
					if ($product != -1){
						$product_info = $invoice->getItemValue($key);
						$products[$key] = $product_info;
						$listingNumber = $product_info['qty'];
						$contract = new SJB_Contract(array('product_sid' => $product, 'numberOfListings' => $listingNumber, 'is_recurring' => $invoice->isRecurring()));
						$contract->setUserSID($user_sid);
						$contract->setPrice($items['amount'][$key]);
						if ($contract->saveInDB()) {
							SJB_ShoppingCart::deleteItemFromCartBySID($product_info['shoppingCartRecord'], $user_sid);
							$bannerInfo = $product_info['banner_info'];
							$paymentStatus = true;
							if ($product_info['product_type'] == 'banners' && !empty($bannerInfo)) {
								$bannersObj = new SJB_Banners();
								$bannersObj->addBanner($bannerInfo['title'], $bannerInfo['link'], $bannerInfo['bannerFilePath'], $bannerInfo['sx'], $bannerInfo['sy'], $bannerInfo['type'], 0, $bannerInfo['banner_group_sid'], $bannerInfo, $user_sid, $contract->getID());
								$bannerGroup = $bannersObj->getBannerGroupBySID($bannerInfo['banner_group_sid']);
								SJB_AdminNotifications::sendAdminBannerAddedLetter($user_sid, $bannerGroup);
							}
							if ($contract->isFeaturedProfile())
								SJB_UserManager::makeFeaturedBySID($user_sid);
							if (SJB_UserNotificationsManager::isUserNotifiedOnSubscriptionActivation($user_sid))
								SJB_Notifications::sendSubscriptionActivationLetter($user_sid, $product_info);
						}
					} else {
						$products[$key] = array('name' => $items['custom_item'][$key]);
						$paymentStatus = true;
					}
				}
				if ($paymentStatus) {
					$invoice->setStatus(SJB_Invoice::INVOICE_STATUS_PAID);
					SJB_InvoiceManager::saveInvoice($invoice);
				}
				$tp->assign('products', $products);
			}
			else {
				$errors['INVOICE_IS_NOT_VERIFIED'] = 1;
			}
		}
		else {
			$errors['INVALID_INVOICE_ID'] = 1;
}
		if(!$errors) {
			$isUserJustRegistered = SJB_UserManager::isCurrentUserJustRegistered();
			if(isset($items['products']) && count($items['products']) == 1 && $isUserJustRegistered && !$userHasContract) {
				$userGroupInfo = SJB_UserGroupManager::getUserGroupInfoBySID($user->getUserGroupSID());
				$pageId = !empty($userGroupInfo['after_registration_redirect_to']) ? $userGroupInfo['after_registration_redirect_to'] : '';
				$redirectUrl = SJB_UserGroupManager::getRedirectUrlByPageID($pageId);
				SJB_HelperFunctions::redirect($redirectUrl);
			}
		}
		$tp->assign('errors', $errors);
		$tp->display('create_contract.tpl');

	}
}
